//
//  ComposeViewCell.swift
//  SHWeibo2.0
//
//  Created by 黄礼潮 on 16/3/4.
//  Copyright © 2016年 黄礼潮. All rights reserved.
//

import UIKit
//通过代理传值按钮点击事件
@objc protocol ComposeViewCellDeledate:NSObjectProtocol{

    optional func addPictureClick(cell:ComposeViewCell)
    optional func removePictureClick(cell:ComposeViewCell)

}

class ComposeViewCell: UICollectionViewCell {
    //定义代理
    weak var delegate:ComposeViewCellDeledate?
    
    var image: UIImage? {
    
        didSet{
            //如果没有图像 隐藏删除按钮
            removepictureBtn.hidden = (image == nil)
            //如果图像为空就显示 默认的加号按钮
            if image == nil{
            
                addPictureBtn.setBackgroundImage(UIImage(named:"compose_pic_add"), forState:.Normal)
                addPictureBtn.setBackgroundImage(UIImage(named:"compose_pic_add_highlighted"), forState: .Highlighted)
                //清空所有的图像
                addPictureBtn.setImage(nil, forState: .Normal)
            }
            //显示图像
            addPictureBtn.setImage(image, forState:.Normal)
            addPictureBtn.setBackgroundImage(UIImage(named:"compose_pic_add"), forState:.Normal)
    
        }
 
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupPictuerUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //添加图片按钮
    private lazy var addPictureBtn:UIButton = UIButton(cz_butitle:nil, backImageName:"compose_pic_add")
    //删去图片按钮
    private lazy var removepictureBtn:UIButton = UIButton(cz_butitle:nil, backImageName:"compose_photo_close")
    
// MARK: - "监听方法"
    @objc private func addPictureButton(){
       print(__FUNCTION__)
        delegate?.addPictureClick?(self)
    }
    @objc private func removePictureButton(){
       print(__FUNCTION__)
        delegate?.removePictureClick?(self)
    }
}


extension ComposeViewCell{
    
    private func setupPictuerUI(){
        
        
        contentView.addSubview(addPictureBtn)
        contentView.addSubview(removepictureBtn)
        
        //自动布局
        let margin = 10
        addPictureBtn.snp_makeConstraints { (make) -> Void in
            //这种四周的约束会造成内存泄露，是第三方框架造成的内存泄露
        //   make.edges.equalTo(UIEdgeInsets(top: 10, left: 10, bottom: -10, right: -10))
            make.left.equalTo(contentView).offset(margin)
            make.right.equalTo(contentView).offset(-margin)
            make.top.equalTo(contentView).offset(margin)
            make.bottom.equalTo(contentView).offset(-margin)
        }
        removepictureBtn.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(addPictureBtn.snp_right)
//            make.centerY.equalTo(addPictureBtn.snp_top)
            make.right.equalTo(contentView)
            make.top.equalTo(contentView)
        }
        
        //添加监听事件
        addPictureBtn.addTarget(self, action:"addPictureButton", forControlEvents:.TouchUpInside)
        removepictureBtn.addTarget(self, action:"removePictureButton", forControlEvents:.TouchUpInside)
        
        //设置按钮的填充模式  一些大的图片 利用这个属性，使图像能等比例的缩放
        addPictureBtn.imageView?.contentMode = .ScaleAspectFill
        
    }
}